
import time
import datetime

class MessageLog:
	def __init__(self):
		self.log = []
		self.handlers = []
		self.i = 0
		self.j = 0
	
	def add_handler(self, handler):
		self.handlers.append(handler)
	
	def broadcast (self, message):
		event = {
			'id' : self.i,
			'time' : time.localtime(),
			'message' : message,
		}
		
		self.i += 1
		self.log.append(event)
		
		for h in self.handlers: h.handle(event)
	
	def tail (self, n):
		return self.log[n:len(self.log)]
	
	def tail_channel (self):
		j = self.j
		self.j = len(self.log)
		return self.log[j:self.j]

